﻿using System;
using System.Data;
using MySSH.IBLL;
using MySSH.Domain.Entities;
using System.Text;
using System.Linq;

namespace MySSH.BLL
{
    public partial class BLLModule : BLLBase<Module>, IBLLModule
    {

        private IDAL.IDALModule _dal;

        public IDAL.IDALModule DAL
        {
            get { return this._dal; }
            set { this._dal = value; base.DAL = this.DAL; }
        }

        public string GetSysMenuByUser(int userID, string loginID, string pwd)
        {
            StringBuilder sb = new StringBuilder();

            DataSet ds = this.DAL.GetSysMenuByUser(userID, loginID, pwd);
            DataTable dt = ds.Tables[0];



            var q = from p in dt.AsEnumerable()
                    where p.Field<int>("ParentID") == 0
                        && (from Parent in dt.AsEnumerable() select Parent.Field<int>("ParentID")).ToList<int>().Contains(p.Field<int>("ID"))
                    select new
                    {
                        title = p.Field<string>("ModuleName"),
                        iconCls = p.Field<string>("IconCls"),
                        ClassID = p.Field<string>("ClassID"),
                        store = new
                        {
                            root = new
                            {
                                children = from c in dt.AsEnumerable()
                                           where c.Field<int>("ParentID") == p.Field<int>("ID")
                                           select new
                                           {
                                               leaf = true,
                                               id = c.Field<int>("ID"),
                                               text = c.Field<string>("ModuleName"),
                                               iconCls = c.Field<string>("IconCls"),
                                               ClassID = c.Field<string>("ClassID"),
                                           }
                            }
                        }
                    };

            string t = Newtonsoft.Json.JsonConvert.SerializeObject(q);
            return t;
            StringBuilder treePanel = new StringBuilder();
            StringBuilder treeNode = new StringBuilder();
            string leaf = "";
            string children = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {

                leaf = Convert.ToBoolean(dr["Leaf"]).ToString().ToLower();
                children = (dr["Children"].ToString() == "" ? "" : ",children:[" + dr["Children"] + "]");

                if (dr["ParentID"] == DBNull.Value || Convert.ToInt32(dr["ParentID"]) == 0)
                    treePanel.AppendFormat("{{title:'{0}',iconCls:'{1}',ClassID:'{2}',store:{{root:{{expanded : true{3}}}}}}},",
                        dr["ModuleName"], dr["iconCls"], dr["ClassID"], children);
                else
                    treeNode.AppendFormat("{0} = {{text:'{1}',iconCls:'{2}',leaf:'{3}',ClassID:'{4}'}};",
                        dr["ClassName"], dr["ModuleName"], dr["iconCls"], leaf, dr["ClassID"]);

            }


            sb.Append(treeNode);
            sb.AppendFormat("Sys.menu = [{0}];", treePanel.ToString().TrimEnd(','));
            return sb.ToString();
        }

        public DataTable GetModelByParentID(int parentID)
        {
            return this.DAL.GetModelByParentID(parentID);
        }
    }
}
